package vip.xiaonuo.biz.modular.pay.util.bankpay;

import cn.hutool.core.bean.BeanUtil;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import vip.xiaonuo.biz.modular.pay.entity.BizPayAccount;
import vip.xiaonuo.biz.modular.pay.entity.BizPayOrder;
import vip.xiaonuo.biz.modular.pay.param.req.BizOrderCheckReq;
import vip.xiaonuo.biz.modular.pay.param.req.BizOrderReq;
import vip.xiaonuo.biz.modular.pay.param.resp.BizPayOrderResp;
import vip.xiaonuo.common.exception.CommonException;

import java.util.Map;

/**
 * ClassName:payClient
 * Description: 支付网关，封装支付的相关请求接口
 *
 * @Author:薛守立
 * @CreateTime:2024/9/18 9:53
 */
@Getter
@Setter
public class PayClient {

    private static Logger logger = LoggerFactory.getLogger(PayClient.class);

    /** 支付请求的固定信息 */
    private BizPayAccount bizPayAccount;

    /** 支付信息的网关 */
    private final static String BANK_URL = "https://payapi.swiftpass.cn/pay/gateway";

    /** 支付接口的回调接口 */
    private final static String CALLBACK_URL = "";

    /** 支付接口的调用方式 */
    private String REQ_METHOD = "XML";

    public PayClient() {
    }

    public PayClient(BizPayAccount bizPayAccount) {
        this.bizPayAccount = bizPayAccount;
    }

    public PayClient(BizPayAccount bizPayAccount, String method) {
        this.bizPayAccount = bizPayAccount;
        this.REQ_METHOD = method;
    }

    /**
     * 响应的验签
     *
     * @Author:薛守立
     * @CreateTime:2024/9/20
     */
    public static boolean verifyResp(String body) {
        // 将响应对象转化为map
        try {
            Map<String,String> resultMap = XmlUtils.toMap(body);
            String reSign = resultMap.get("sign");
            String sign_type = resultMap.get("sign_type");
            boolean res =  SignUtil.verifySign(reSign, sign_type, resultMap)&& reSign!=null ;
            if(!res) throw new CommonException("接口响应加密信息验证失败");
            return true;
        }catch (Exception e){
            throw new CommonException("接口响应加密信息解析失败");
        }
    }

    /**
     * 创建订单
     *
     * @Author:薛守立
     * @CreateTime:2024/9/20
     */
    public static BizPayOrderResp orderRequest(BizPayOrder bizPayOrder){
        XmlRequest<BizOrderReq> xmlRequest = new XmlRequest<BizOrderReq>();
        // 构建请求参数
        BizOrderReq bizOrderReq = BeanUtil.toBean(bizPayOrder, BizOrderReq.class);
        bizOrderReq.setNotify_url(CALLBACK_URL);
        ResponseEntity<String> response = xmlRequest.postRequest(bizOrderReq, BANK_URL);
        // 此处验签
        verifyResp(response.getBody());
        // 验证通过返回结果
        return XmlUtils.xmlToJavaBean(response.getBody());
    }


    /**
     * 订单查询(含退款查询)
     *
     * @Author:薛守立
     * @CreateTime:2024/9/20
     */
    public static BizPayOrderResp checkOrder(BizPayOrder bizPayOrder, String method){
        XmlRequest<BizOrderCheckReq> xmlRequest = new XmlRequest<BizOrderCheckReq>();
        // 构建请求参数
        BizOrderCheckReq req = BeanUtil.toBean(bizPayOrder, BizOrderCheckReq.class);
        if(method.equals("query"))  req.setService("unified.trade.query");
        else req.setService("unified.trade.refundquery");
        ResponseEntity<String> response = xmlRequest.postRequest(req, BANK_URL);
        // 此处验签
        verifyResp(response.getBody());
        // 验证通过返回结果
        return XmlUtils.xmlToJavaBean(response.getBody());
    }

    /**
     * 关闭订单
     *
     * @Author:薛守立
     * @CreateTime:2024/9/20
     */
    public static BizPayOrderResp closeOrder(BizPayOrder bizPayOrder){
        XmlRequest<BizOrderReq> xmlRequest = new XmlRequest<BizOrderReq>();
        // 构建请求参数
        BizOrderReq bizOrderReq = BeanUtil.toBean(bizPayOrder, BizOrderReq.class);
        ResponseEntity<String> response = xmlRequest.postRequest(bizOrderReq, BANK_URL);
        // 此处验签
        verifyResp(response.getBody());
        // 验证通过返回结果
        return XmlUtils.xmlToJavaBean(response.getBody());
    }

    /**
     * 订单退款
     *
     * @Author:薛守立
     * @CreateTime:2024/9/20
     */
    public static BizPayOrderResp backOrder(BizPayOrder bizPayOrder){
        XmlRequest<BizOrderReq> xmlRequest = new XmlRequest<BizOrderReq>();
        // 构建请求参数
        BizOrderReq bizOrderReq = BeanUtil.toBean(bizPayOrder, BizOrderReq.class);
        ResponseEntity<String> response = xmlRequest.postRequest(bizOrderReq, BANK_URL);
        // 此处验签
        verifyResp(response.getBody());
        // 验证通过返回结果
        return XmlUtils.xmlToJavaBean(response.getBody());
    }

}
